package com.factory.test.activity;

import android.os.Bundle;
import android.widget.SeekBar;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivityLamplightBinding;
import com.yecontech.sdk.base.gpio.GpioLampLight;
import com.yecontech.sdk.base.vo.Constants;

/**
 * 灯光测试
 */
public class LamplightActivity extends BaseTestActivity {

    private ActivityLamplightBinding binding;
    private GpioLampLight gpioLampLight;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_lamplight);
        binding.setViewClick(new ViewClick());

        gpioLampLight = sdkService.getGpioLampLightManager();
        gpioLampLight.off();

        binding.seekBar.setEnabled(false);
        binding.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(lampLightMode != null){
                    updateMessage(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private Constants.LampLightMode lampLightMode;
    public class ViewClick{

        public void onOpenWhite(){
            if(binding.white.isChecked()){
                gpioLampLight.onWhite();
                lampLightMode = Constants.LampLightMode.white;
                binding.seekBar.setProgress(50);
                binding.seekBar.setEnabled(true);
            }else{
                binding.seekBar.setProgress(0);
                gpioLampLight.off();
                lampLightMode = null;
                binding.seekBar.setEnabled(false);
            }
        }


        public void onOpenRed(){
            if(binding.red.isChecked()){
                binding.white.setChecked(false);
                binding.green.setChecked(false);
                gpioLampLight.onRed();
                lampLightMode = Constants.LampLightMode.red;
                binding.seekBar.setProgress(50);
                binding.seekBar.setEnabled(true);
            }else{
                binding.seekBar.setProgress(0);
                gpioLampLight.off();
                lampLightMode = null;
                binding.seekBar.setEnabled(false);
            }
        }


        public void onOpenGreen(){
            if(binding.green.isChecked()){
                gpioLampLight.onGreen();
                binding.white.setChecked(false);
                binding.red.setChecked(false);
                lampLightMode = Constants.LampLightMode.green;
                binding.seekBar.setProgress(50);
                binding.seekBar.setEnabled(true);
            }else{
                binding.seekBar.setProgress(0);
                gpioLampLight.off();
                lampLightMode = null;
                binding.seekBar.setEnabled(false);
            }
        }
    }




    private void updateMessage(int p){
        binding.message.setText(String.format("亮度调节: %d%%", p));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        gpioLampLight.off();
    }
}
